<?php
/**
 * @title 5534. 可见点的最大数目 - 中心角度
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $points
     * @param Integer $angle
     * @param Integer[] $location
     * @return Integer
     */
    function visiblePoints($points, $angle, $location) {
        /**
         * @since 2020-10-10 角度
         */
        $angle = deg2rad($angle);
        $angleArray = [];
        // return $angle;

        /**
         * @since 2020-10-10 原点数量
         */
        $originCount = 0;

        /**
         * @since 2020-10-10 坐标中心转移到location
         */
        foreach ($points as $key=>$point){
            $x = $point[0] - $location[0];
            $y = $point[1] - $location[1];

            /**
             * @since 2020-10-10 坐标轴
             */
            if($x == 0){
                if($y == 0){
                    /**
                     * @since 2020-10-10 在原点, 任意位置可以看
                     */
                    $originCount++;
                } elseif($y > 0) {
                    /**
                     * @since 2020-10-10 90°
                     */
                    $angleArray[] = M_PI_2;
                } else {
                    /**
                     * @since 2020-10-10 270°
                     */
                    $angleArray[] = M_PI + M_PI_2;
                }
            } elseif($y == 0) {
                if($x > 0){
                    /**
                     * @since 2020-10-10 0°
                     */
                    $angleArray[] = 0;
                } else {
                    /**
                     * @since 2020-10-10 180°
                     */
                    $angleArray[] = M_PI;
                }
            } else {
                /**
                 * @since 2020-10-10 第一象限
                 */
                if($x > 0 AND $y > 0){
                    $tan = $y/$x;
                    $angleNum = atan($tan);
                } elseif($x < 0 AND $y > 0) {
                    /**
                     * @since 2020-10-10 第二象限
                     */
                    $tan = -$y/$x;
                    $angleNum = atan($tan);

                    $angleNum = M_PI-$angleNum;
                } elseif($x < 0 AND $y < 0){
                    /**
                     * @since 2020-10-10 第三象限
                     */
                    $tan = $y/$x;
                    $angleNum = atan($tan);

                    $angleNum = M_PI+$angleNum;
                } else {
                    /**
                     * @since 2020-10-10 第四象限
                     */
                    $tan = -$y/$x;
                    $angleNum = atan($tan);

                    $angleNum = M_PI*2-$angleNum;
                }

                /**
                 * @since 2020-10-10 角度
                 */
                $angleArray[] = $angleNum;
            }
        }

        /**
         * @since 2020-10-10 角度排序
         */
        if(empty($angleArray)){
            return $originCount;
        } else {
            sort($angleArray);
        }
        // var_dump($angleArray);
        // return [$originCount, $angleArray];
        // return $angleArray;

        /**
         * @since 2020-10-10 每个角度依次作为可视角最边缘起点
         */
        $len = count($angleArray);
        $lastAngle = null;
        $max = 1;
        // echo "angle={$angle}", PHP_EOL;
        foreach ($angleArray as $key=>$a){
            /**
             * @since 2020-10-10 角度一样跳过
             */
            if($a === $lastAngle){
                continue;
            } else {
                $lastAngle = $a;
            }

            /**
             * @since 2020-10-10 往后找角度
             */
            for($j = $max; $j <= $len-1; $j++){
                $k = ($key+$j)%$len;
                $b = $angleArray[$k];

                /**
                 * @since 2020-10-10 a到b角度的夹角d
                 */
                if($b>=$a){
                    $d = $b-$a;
                } else {
                    $d = $b+M_PI*2-$a;
                }

                /**
                 * @since 2020-10-10 夹角是否在范围内
                 */
                if($d <= $angle){
                    // echo "[{$key}]={$a}, [{$k}]={$b}, d={$d}, max=", $j+1, PHP_EOL;
                    $max = $j+1;
                } else {
                    break;
                }
            }
        }

        /**
         * @return
         */
        return $max+$originCount;
    }
}

/**
 * @since 2020-10-10 大数据量
 */
$points = [];
for($i=0; $i<=10000; $i++){
    $points[] = [mt_rand(), mt_rand()];
}


/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.04_4.php
 */
$datas = [
    [
        $points,
        49,
        [0,0],
    ],
    // [
    //     [[1,1],[2,2],[3,3],[4,4],[1,2],[2,1]], 0, [1,1]
    // ],
    // [
    //     [[2,1],[2,2],[3,3]], 90, [1,1]
    // ],
    // [
    //     [[2,1],[2,2],[3,4],[1,1]], 90, [1,1]
    // ],
    // [
    //     [[1,0],[2,1]], 13, [1,1],
    // ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof(2);
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->visiblePoints(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    // var_dump($result);
    if(count($result)<=20){
        var_dump($result);
    } else {
        var_dump(count($result));
    }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
